package xyz.thoughtset.viewer.common.core.page;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

/**
 * 对分页的基本数据进行封装
 */
@Data
@NoArgsConstructor
public class Page<T> {
    
    private int pageNum = 1;//页码，默认是第一页
    private int pageSize = 10;
    private long total;//总记录数
    private long totalPage;//总页数
//    private Flux<T> results = Flux.empty();
    private List<T> results = new ArrayList<T>();
    private T obj;
	
    
    public Page(int pageSize, int pageNum) {
        this.pageSize = pageSize>0?pageSize:1;
        this.pageNum = pageNum>0?pageNum:0;
    }



    public Page setTotalAndResults(Long count, List<T> list) {
        this.total = count==null ? 0 : count;
        this.results =list;
        computeTotalPage();
        return this;
    }


    private void computeTotalPage(){
        this.totalPage = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
    }

    public int getPageNum() {
        return pageNum>0?pageNum:0;
    }

    public int getPageSize() {
        return pageSize>0?pageSize:1;
    }

    public T getObj() {
		return obj;
	}

	public void setObj(T obj) {
		this.obj = obj;
	}
	
}